#!/bin/bash
set -euo pipefail

# ArXiv Scraper Service Installation Script
# This script installs and configures the ArXiv scraper service for production use

# Configuration
SERVICE_USER="arxiv-scraper"
SERVICE_GROUP="arxiv-scraper"
INSTALL_DIR="/opt/arxiv-scraper-service"
SERVICE_NAME="arxiv-scraper"
PYTHON_VERSION="3.9"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

log() {
    echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
}

warn() {
    echo -e "${YELLOW}[$(date +'%Y-%m-%d %H:%M:%S')] WARNING: $1${NC}"
}

error() {
    echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: $1${NC}"
    exit 1
}

# Check if running as root
if [[ $EUID -ne 0 ]]; then
    error "This script must be run as root"
fi

# Check OS
if [[ ! -f /etc/os-release ]]; then
    error "Cannot determine OS. /etc/os-release not found."
fi

source /etc/os-release
log "Detected OS: $PRETTY_NAME"

# Install system dependencies
log "Installing system dependencies..."

if [[ "$ID" == "ubuntu" ]] || [[ "$ID" == "debian" ]]; then
    apt-get update
    apt-get install -y \
        python3 \
        python3-pip \
        python3-venv \
        python3-dev \
        build-essential \
        libpq-dev \
        postgresql-client \
        supervisor \
        nginx \
        curl \
        wget \
        git \
        htop \
        logrotate
        
elif [[ "$ID" == "centos" ]] || [[ "$ID" == "rhel" ]] || [[ "$ID" == "fedora" ]]; then
    if command -v dnf &> /dev/null; then
        dnf install -y \
            python3 \
            python3-pip \
            python3-devel \
            gcc \
            gcc-c++ \
            postgresql-devel \
            postgresql \
            supervisor \
            nginx \
            curl \
            wget \
            git \
            htop \
            logrotate
    else
        yum install -y \
            python3 \
            python3-pip \
            python3-devel \
            gcc \
            gcc-c++ \
            postgresql-devel \
            postgresql \
            supervisor \
            nginx \
            curl \
            wget \
            git \
            htop \
            logrotate
    fi
else
    warn "Unsupported OS: $ID. Attempting to continue..."
fi

# Create service user and group
log "Creating service user and group..."
if ! getent group "$SERVICE_GROUP" > /dev/null; then
    groupadd --system "$SERVICE_GROUP"
fi

if ! getent passwd "$SERVICE_USER" > /dev/null; then
    useradd --system --gid "$SERVICE_GROUP" --home-dir "$INSTALL_DIR" \
            --shell /bin/bash --create-home "$SERVICE_USER"
fi

# Create installation directory
log "Creating installation directory..."
mkdir -p "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR/data"
mkdir -p "$INSTALL_DIR/logs"
mkdir -p "$INSTALL_DIR/backups"

# Copy service files
log "Copying service files..."
cp -r . "$INSTALL_DIR/"
chown -R "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR"
chmod +x "$INSTALL_DIR/main.py"
chmod +x "$INSTALL_DIR/cli.py"

# Create Python virtual environment
log "Creating Python virtual environment..."
sudo -u "$SERVICE_USER" python3 -m venv "$INSTALL_DIR/venv"

# Install Python dependencies
log "Installing Python dependencies..."
sudo -u "$SERVICE_USER" "$INSTALL_DIR/venv/bin/pip" install --upgrade pip
sudo -u "$SERVICE_USER" "$INSTALL_DIR/venv/bin/pip" install -r "$INSTALL_DIR/requirements.txt"

# Install systemd service files
log "Installing systemd service files..."
cp "$INSTALL_DIR/deploy/systemd/arxiv-scraper.service" /etc/systemd/system/
cp "$INSTALL_DIR/deploy/systemd/arxiv-scraper-scheduler.service" /etc/systemd/system/
systemctl daemon-reload

# Create configuration file if it doesn't exist
if [[ ! -f "$INSTALL_DIR/config.yaml" ]]; then
    log "Creating default configuration..."
    sudo -u "$SERVICE_USER" cp "$INSTALL_DIR/config.yaml" "$INSTALL_DIR/config.production.yaml"
    
    warn "Please edit $INSTALL_DIR/config.yaml with your database credentials"
    warn "and other configuration settings before starting the service."
fi

# Set up log rotation
log "Setting up log rotation..."
cat > /etc/logrotate.d/arxiv-scraper << EOF
$INSTALL_DIR/logs/*.log {
    daily
    missingok
    rotate 30
    compress
    delaycompress
    notifempty
    copytruncate
    su $SERVICE_USER $SERVICE_GROUP
}
EOF

# Create systemd drop-in directory for overrides
mkdir -p /etc/systemd/system/arxiv-scraper.service.d
mkdir -p /etc/systemd/system/arxiv-scraper-scheduler.service.d

# Set up basic monitoring script
log "Creating monitoring script..."
cat > "$INSTALL_DIR/monitor.sh" << 'EOF'
#!/bin/bash
# Simple monitoring script for ArXiv scraper service

SERVICE_NAME="arxiv-scraper"
LOG_FILE="/opt/arxiv-scraper-service/logs/monitor.log"

echo "$(date): Checking service status..." >> "$LOG_FILE"

if systemctl is-active --quiet "$SERVICE_NAME"; then
    echo "$(date): Service is running" >> "$LOG_FILE"
else
    echo "$(date): Service is not running, attempting restart..." >> "$LOG_FILE"
    systemctl restart "$SERVICE_NAME"
    
    if systemctl is-active --quiet "$SERVICE_NAME"; then
        echo "$(date): Service restarted successfully" >> "$LOG_FILE"
    else
        echo "$(date): Failed to restart service" >> "$LOG_FILE"
    fi
fi
EOF

chmod +x "$INSTALL_DIR/monitor.sh"
chown "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR/monitor.sh"

# Set up crontab for monitoring (optional)
log "Setting up monitoring cron job..."
(crontab -u "$SERVICE_USER" -l 2>/dev/null; echo "*/5 * * * * $INSTALL_DIR/monitor.sh") | crontab -u "$SERVICE_USER" -

# Create environment file for systemd services
log "Creating environment configuration..."
cat > /etc/default/arxiv-scraper << EOF
# ArXiv Scraper Service Environment Variables
# Edit this file to customize the service configuration

# Database Configuration
DATABASE_URL="postgresql://arxiv_user:arxiv_password@localhost:5432/arxiv_papers"

# Logging Configuration
LOG_LEVEL="INFO"

# Scraping Configuration
SCRAPING_RATE_LIMIT=3.0
SCRAPING_MAX_RETRIES=5

# Monitoring Configuration
MONITORING_ENABLED=true

# Scheduling Configuration
SCHEDULING_ENABLED=false
DAILY_RUN_TIME="02:00"

# Performance Configuration
SCRAPING_BATCH_SIZE=100
PROCESSING_BATCH_SIZE=50
EOF

# Enable but don't start services yet
log "Enabling systemd services..."
systemctl enable arxiv-scraper.service
systemctl enable arxiv-scraper-scheduler.service

# Create health check script
log "Creating health check script..."
cat > "$INSTALL_DIR/health-check.sh" << 'EOF'
#!/bin/bash
# Health check script for ArXiv scraper service

INSTALL_DIR="/opt/arxiv-scraper-service"
cd "$INSTALL_DIR"

# Run health check
"$INSTALL_DIR/venv/bin/python" cli.py health

if [[ $? -eq 0 ]]; then
    echo "Health check passed"
    exit 0
else
    echo "Health check failed"
    exit 1
fi
EOF

chmod +x "$INSTALL_DIR/health-check.sh"
chown "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR/health-check.sh"

# Final permissions check
log "Setting final permissions..."
chown -R "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR"
chmod -R 755 "$INSTALL_DIR"
chmod -R 644 "$INSTALL_DIR/logs" 2>/dev/null || true
chmod -R 644 "$INSTALL_DIR/data" 2>/dev/null || true

log "Installation completed successfully!"
log ""
log "Next steps:"
log "1. Edit the configuration file: $INSTALL_DIR/config.yaml"
log "2. Set up your PostgreSQL database"
log "3. Configure database connection in: /etc/default/arxiv-scraper"
log "4. Start the service: systemctl start arxiv-scraper"
log "5. Check status: systemctl status arxiv-scraper"
log "6. View logs: journalctl -u arxiv-scraper -f"
log ""
log "For scheduling, enable the scheduler service:"
log "  systemctl start arxiv-scraper-scheduler"
log ""
log "CLI usage:"
log "  sudo -u $SERVICE_USER $INSTALL_DIR/venv/bin/python $INSTALL_DIR/cli.py --help"
log ""
log "Health check:"
log "  $INSTALL_DIR/health-check.sh"

warn "Don't forget to:"
warn "1. Configure your database connection string"
warn "2. Set up proper firewall rules"
warn "3. Configure SSL certificates if needed"
warn "4. Set up monitoring and alerting"